package com.ruoyi.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.bo.TFileInfoBo;
import com.ruoyi.system.domain.vo.TFileInfoVo;
import com.ruoyi.system.service.ITFileInfoService;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 图片文件保存
 *
 * @author ruoyi
 * @date 2023-08-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/fileInfo")
public class TFileInfoController extends BaseController {

    private final ITFileInfoService iTFileInfoService;

    /**
     * 查询图片文件保存列表
     */
    @SaCheckPermission("system:fileInfo:list")
    @GetMapping("/list")
    public TableDataInfo<TFileInfoVo> list(TFileInfoBo bo, PageQuery pageQuery) {
        return iTFileInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出图片文件保存列表
     */
    @SaCheckPermission("system:fileInfo:export")
    @Log(title = "图片文件保存", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TFileInfoBo bo, HttpServletResponse response) {
        List<TFileInfoVo> list = iTFileInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "图片文件保存", TFileInfoVo.class, response);
    }

    /**
     * 获取图片文件保存详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:fileInfo:query")
    @GetMapping("/{id}")
    public R<TFileInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iTFileInfoService.queryById(id));
    }

    /**
     * 新增图片文件保存
     */
    @SaCheckPermission("system:fileInfo:add")
    @Log(title = "图片文件保存", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TFileInfoBo bo) {
        return toAjax(iTFileInfoService.insertByBo(bo));
    }

    /**
     * 修改图片文件保存
     */
    @SaCheckPermission("system:fileInfo:edit")
    @Log(title = "图片文件保存", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TFileInfoBo bo) {
        return toAjax(iTFileInfoService.updateByBo(bo));
    }

    /**
     * 删除图片文件保存
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:fileInfo:remove")
    @Log(title = "图片文件保存", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iTFileInfoService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
